<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        // 26. <数组练习>数组转换
        const input = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15];
        const output = [
            {
                position: [1, 2, 3],
            },
            {
                position: [4, 5, 6],
            },
            {
                position: [7, 8, 9],
            },
            {
                position: [10, 11, 12],
            },
            {
                position: [13, 14, 15],
            },
        ];

        // ⭐ 方法1
        function transform(input) {
            const res = [];
            for (let i = 0; i < input.length; i += 3){
                res.push({
                    position:[input[i], input[i + 1], input[i + 2]],
                })
            }
            return res;
        }
        console.log(transform(input));

        // ⭐ 方法2
        function transform1(input) {
            const res = [];
            for (let i = 0; i < input.length; i += 3){
                res.push({
                    position: input.slice(i, i + 3),
                })
            }
            return res;
        }
        console.log(transform1(input));
        

        
        const arr = [1,2,3,4,5,6,7,8];
        // 从第几个索引开始截，截到总长度的第几个
        console.log(arr.slice(2,4));// [3, 4]
        // 从第几个索引开始截，截多少个
        console.log(arr.splice(3,4))// [4, 5, 6, 7]
    </script>
</body>
</html>